package com.szewec.ps.util.imagepicker;

import android.app.Activity;
import android.net.Uri;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.szewec.ps.R;
import com.szewec.ps.util.imagepicker.utils.ImageLoader;
import com.szewec.ps.base.BaseApplication;

import java.io.File;

/**
 * 实现自定义图片加载
 * Create by: chenWei.li
 * Date: 2018/8/30
 * Time: 下午11:10
 * Email: lichenwei.me@foxmail.com
 */
public class GlideLoader implements ImageLoader {

    private RequestOptions mOptions = new RequestOptions()
            .centerCrop()
            .dontAnimate()//图片在占位图的同时下载并实时显示图片,关闭动画
            .format(DecodeFormat.PREFER_RGB_565)
//            .placeholder(R.mipmap.icon_image_default)
            .error(R.mipmap.icon_image_error);

    private RequestOptions mPreOptions = new RequestOptions()
//            .skipMemoryCache(true)//是否缓存到内存,true禁用内存缓存
//            .diskCacheStrategy(DiskCacheStrategy.ALL)//来缓存所有不同形状的图片
            .error(R.mipmap.icon_image_error);

    @Override
    public void loadImage(ImageView imageView, String imagePath) {
        //小图加载
        Glide.with(imageView.getContext())
                .load(imagePath)
                .apply(mOptions)
                .thumbnail(0.1f)
                .into(imageView);
    }

    @Override
    public void loadThumbnail(int type, ImageView imageView, String imagePath) {
        //小图加载
        if(type == 1 && TextUtils.isEmpty(imagePath)){//如果是视频文件且视频没有第一帧
            Glide.with(imageView.getContext())
                    .load(R.drawable.video_play_tip)
                    .apply(mOptions)
                    .thumbnail(0.1f)
                    .into(imageView);
        }else {
            Glide.with(imageView.getContext())
                    .load(imagePath)
                    .apply(mOptions)
                    .thumbnail(0.1f)
                    .into(imageView);
        }
    }

    @Override
    public void loadPreImage(ImageView imageView, String imagePath) {
        //大图加载
        Glide.with(imageView.getContext())
                .load(imagePath)
                .apply(mPreOptions)
                .thumbnail(Glide.with(imageView).load(R.drawable.loading_gif3))//解决gif占位图不生效问题
                .into(imageView);

    }

    @Override
    public void displayImage(Activity activity, String path, ImageView imageView, int width, int height) {
        Glide.with(activity)                             //配置上下文
                .load(Uri.fromFile(new File(path)))      //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                .error(R.drawable.ic_default_image)           //设置错误图片
                .placeholder(R.drawable.ic_default_image)     //设置占位图片
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .into(imageView);
    }

    @Override
    public void displayImagePreview(Activity activity, String path, ImageView imageView, int width, int height) {
        Glide.with(activity)                             //配置上下文
                .load(Uri.fromFile(new File(path)))      //设置图片路径(fix #8,文件名包含%符号 无法识别和显示)
                .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存全尺寸
                .into(imageView);
    }

    @Override
    public void clearMemoryCache() {
        //清理缓存
        Glide.get(BaseApplication.getContext()).clearMemory();
    }

}
